﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using log4net;
using NHibernate.Criterion;
using BidFunLib.Entity;
using BidFunLib.DAO;


namespace BidFunLib
{
    /// <summary>
    /// 
    /// </summary>
    class StatsBidUserByBidSaleDAO 
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(StatsBidUserByBidSaleDAO));

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public StatsBidUserByBidSale getStatsBidUserByBidSaleById(Int32 id)
        {
            StatsBidUserByBidSale statsBidUserByBidSale = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<StatsBidUserByBidSale> statsBidUserByBidSaleList = (IList<StatsBidUserByBidSale>)session.CreateQuery("from StatsBidUserByBidSale bs where bs.IdStatsBidUserByBidSale= ?")
                .SetInt32(0, id)
                .List<StatsBidUserByBidSale>();

                if (statsBidUserByBidSaleList != null && statsBidUserByBidSaleList.Count > 0)
                {
                    statsBidUserByBidSale = statsBidUserByBidSaleList.First();
                }
                transaction.Commit();
                session.Close();
                return statsBidUserByBidSale;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pseudo"></param>
        /// <returns></returns>
        public StatsBidUserByBidSale saveOrUpdate(StatsBidUserByBidSale statsBidUserByBidSale)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                if (statsBidUserByBidSale.IdStatsBidUserByBidSale == -1)
                {
                    log.Debug("Saving StatsBidUserByBidSale...");
                    Int32 newID = (Int32)session.Save(statsBidUserByBidSale);
                    statsBidUserByBidSale.IdStatsBidUserByBidSale = newID;
                    log.Debug("StatsBidUserByBidSale saved !");
                }
                else
                {
                    log.Debug("Updating StatsBidUserByBidSale...");
                    session.Update(statsBidUserByBidSale);
                    log.Debug("StatsBidUserByBidSale updated !");
                }
                transaction.Commit();
                session.Close();
                return statsBidUserByBidSale;
            }
        }


    }
}
